package com.hrprojectback.controller.XinXiGuanLi;

import com.alibaba.fastjson.JSON;
import com.hrprojectback.dto.*;
import com.hrprojectback.enums.StatusEnum;
import com.hrprojectback.service.XinXiGuanLiService.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping("/role")
    public ResponseDTO getUser(RoleInfo roleInfo) {
        //获取角色的总数量
        Integer roleCounts = roleService.getRoleCounts(roleInfo.getRoleSign(), roleInfo.getName());
        //开始页
        int pageStart = (roleInfo.getPageNum() - 1) * roleInfo.getPageSize();
        List<Role> roles = roleService.getRole(roleInfo.getRoleSign(), roleInfo.getName(), pageStart, roleInfo.getPageSize());
        //如果角色不存在
        if (roles.size() == 0) {
            return Response.error3(StatusEnum.STAFF_NOT_EXIST4.getCode(), StatusEnum.STAFF_NOT_EXIST4.getMessage(), roles);
        }
        HashMap<String, Object> res = new HashMap<>();
        res.put("roleCounts", roleCounts);
        res.put("data", roles);
        return Response.success(res);
    }

    /**
     * 新增角色
     *
     * @param role
     * @return
     */
    @PostMapping("/addRole")
    public ResponseDTO addRole(@RequestBody Role role) {
        //默认是生效
        role.setStatus("1");
        int i = roleService.addRole(role);

        if (i > 0) {
            return Response.success(role);
        }
        return Response.error();
    }

    /**
     * 根据角色标识去查询需要编辑的角色信息
     *
     * @param roleSign
     * @return
     */
    @RequestMapping("/getEditRole")
    public String editRole(String roleSign) {
        //获取需要修改的角色的信息
        Role getEditRole = roleService.getEditRole(roleSign);
        String editRole = JSON.toJSONString(getEditRole);
        return editRole;
    }

    /**
     * 编辑角色信息
     *
     * @param role
     * @return
     */
    @PutMapping("/editRole")
    public ResponseDTO editRole(@RequestBody Role role) {
        int i = roleService.editRole(role);
        if (i > 0) {
            return Response.success(role);
        }
        return Response.error();
    }

    /**
     * 根据角色标识roleSign删除角色
     *
     * @param roleSign
     * @return
     */
    @DeleteMapping("/deleteRoleSign")
    public ResponseDTO deleteRole(String roleSign) {
        int i = roleService.deleteRole(roleSign);
        if (i > 0) {
            return Response.success();
        }
        return Response.error();
    }

    /**
     * 根据角色标识roleSign生效员工
     *
     * @param roleSign
     * @param status
     * @return
     */
    @RequestMapping("/roleStateShengXiao")
    public ResponseDTO roleStateShengXiao(@RequestParam("roleSign") String roleSign, @RequestParam("status") int status) {
        if (status == 1) {
            return Response.error(StatusEnum.STAFF_IS_ROLE_SHENG_XIAO);
        }
        int shengXiao = roleService.ShengXiao(roleSign);
        if (shengXiao > 0) {
            return Response.successStatus(shengXiao);
        }
        return Response.error(StatusEnum.STATUS_SHENG_XIAO_ERROR);
    }

    /**
     * 根据角色标识roleSign失效员工
     *
     * @param roleSign
     * @param status
     * @return
     */
    @RequestMapping("/roleStateShiXiAo")
    public ResponseDTO roleStateShiXiAo(@RequestParam("roleSign") String roleSign, @RequestParam("status") int status) {
        if (status == 0) {
            return Response.error(StatusEnum.STAFF_IS_ROLE_SHI_XIAO);
        }
        int shiXiAo = roleService.YiShiXiAo(roleSign);
        if (shiXiAo > 0) {
            return Response.successStatus2(shiXiAo);
        }
        return Response.error(StatusEnum.STATUS_SHI_XIAO_ERROR);
    }

    /**
     * 通过角色标识批量删除员工
     *
     * @param roleSigns
     * @return
     */
    @DeleteMapping("/BatchDeleteRole")
    public ResponseDTO BatchDeleteRole(@RequestBody List<String> roleSigns) {
        if (roleSigns.size() > 0) {
            for (String roleSign : roleSigns) {
                deleteRole(roleSign);
            }
            return Response.success();
        }
        return Response.error();
    }

    /**
     * 通过角色标识批量生效员工
     *
     * @param roleSigns
     * @return
     */
    @RequestMapping("/BatchShengXiaoRole")
    public ResponseDTO BatchShengXiaoRole(@RequestBody List<String> roleSigns) {
        if (roleSigns.size() == 0) {
            return Response.success();
        }
        if (roleSigns.size() > 0) {
            ArrayList statusList = new ArrayList();
            for (String roleSign : roleSigns) {
                //查看角色的当前状态
                int roleStatus = roleService.getStatus(roleSign);
                if (roleStatus == 1) {
                    statusList.add(roleStatus);
                }
            }
            if (statusList.size() > 0) {
                return Response.error(StatusEnum.SHENG_XIAO_ROLE_IS_EXIST);
            }
            for (String roleSign : roleSigns) {
                roleService.ShengXiao(roleSign);
            }
            return Response.success();
        }
        return Response.error();
    }

    /**
     * 通过角色标识批量失效员工
     *
     * @param roleSigns
     * @return
     */
    @RequestMapping("/BatchShiXiaoRole")
    public ResponseDTO BatchShiXiaoRole(@RequestBody List<String> roleSigns) {
        if (roleSigns.size() == 0) {
            return Response.success();
        }
        if (roleSigns.size() > 0) {
            ArrayList statusList = new ArrayList();
            for (String roleSign : roleSigns) {
                //查看角色的当前状态
                int userStatus = roleService.getStatus(roleSign);
                if (userStatus == 0) {
                    statusList.add(userStatus);
                }
            }
            if (statusList.size() > 0) {
                return Response.error(StatusEnum.SHI_XIAO_ROLE_IS_EXIST);
            }
            for (String roleSign : roleSigns) {
                roleService.YiShiXiAo(roleSign);
            }
            return Response.success();
        }
        return Response.error();
    }
}
